廖雪峰Python教程疑问

1. 字符串的strip()方法

原文:

“把一个序列中的空字符串删掉,可以这么写:

def not_empty(s):

   return s and s.strip()

list(filter(not_empty, ['A', '', 'B', None, 'C', ' ']))

# 结果: ['A', 'B', 'C']”

疑问:如果改为,

def not_empty(s):

   return s.strip()

就会提示错误,AttributeError: 'Nonetype' object has no attribute 'strip'

不知道 s and s.strip() 与 s.strip()的区别在哪里。

出处:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431821084171d2e0f22e7cc24305ae03aa0214d0ef29000

 

posted @ 2016-08-25 09:06  Abraham_Xu  阅读(345)  评论(0编辑  收藏  举报